import java.util.Scanner;

public class jinzhizhuanhuan {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int[] c=new int[n];
        String d=" ";
        String e=" ";
        for (int i = 0; i <n; i++) {
            String a=in.next();
            int b=Integer.valueOf(a,16);
            d=d+Integer.toOctalString(b)+e;
        }
        char[] f=d.toCharArray();
        for (int i = 1; i < f.length; i++) {
            System.out.print(f[i]);
            if(f[i]==' ') System.out.println();
        }
    }
}
